{
 "cells": [
  {
   "cell_type": "code",
   "id": "7815b9083fdbfc21",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T02:01:18.441168Z",
     "start_time": "2025-09-30T02:01:18.429635Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn.metrics import accuracy_score\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ],
   "outputs": [],
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "id": "9218e9363d74b525",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T02:01:20.311696Z",
     "start_time": "2025-09-30T02:01:20.245606Z"
    }
   },
   "source": [
    "dataset = pd.read_csv('../../dataset/运动会数据集.csv', encoding='gbk')\n",
    "dataset"
   ],
   "outputs": [
    {
     "data": {
      "text/plain": [
       "                比赛类别          比赛时间 参与者             国家  奖牌\n",
       "0                 冰壶  beijing-2022  团体          Italy  金奖\n",
       "1                 冰壶  beijing-2022  团体          Italy  金奖\n",
       "2                 冰壶  beijing-2022  团体         Norway  银奖\n",
       "3                 冰壶  beijing-2022  团体         Norway  银奖\n",
       "4                 冰壶  beijing-2022  团体         Sweden  铜奖\n",
       "...              ...           ...  ..            ...  ..\n",
       "21692  Weightlifting   athens-1896  个人        Denmark  银奖\n",
       "21693  Weightlifting   athens-1896  个人         Greece  铜奖\n",
       "21694  Weightlifting   athens-1896  个人        Denmark  金奖\n",
       "21695  Weightlifting   athens-1896  个人  Great Britain  银奖\n",
       "21696  Weightlifting   athens-1896  个人         Greece  铜奖\n",
       "\n",
       "[21697 rows x 5 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>比赛类别</th>\n",
       "      <th>比赛时间</th>\n",
       "      <th>参与者</th>\n",
       "      <th>国家</th>\n",
       "      <th>奖牌</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>冰壶</td>\n",
       "      <td>beijing-2022</td>\n",
       "      <td>团体</td>\n",
       "      <td>Italy</td>\n",
       "      <td>金奖</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>冰壶</td>\n",
       "      <td>beijing-2022</td>\n",
       "      <td>团体</td>\n",
       "      <td>Italy</td>\n",
       "      <td>金奖</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>冰壶</td>\n",
       "      <td>beijing-2022</td>\n",
       "      <td>团体</td>\n",
       "      <td>Norway</td>\n",
       "      <td>银奖</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>冰壶</td>\n",
       "      <td>beijing-2022</td>\n",
       "      <td>团体</td>\n",
       "      <td>Norway</td>\n",
       "      <td>银奖</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>冰壶</td>\n",
       "      <td>beijing-2022</td>\n",
       "      <td>团体</td>\n",
       "      <td>Sweden</td>\n",
       "      <td>铜奖</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21692</th>\n",
       "      <td>Weightlifting</td>\n",
       "      <td>athens-1896</td>\n",
       "      <td>个人</td>\n",
       "      <td>Denmark</td>\n",
       "      <td>银奖</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21693</th>\n",
       "      <td>Weightlifting</td>\n",
       "      <td>athens-1896</td>\n",
       "      <td>个人</td>\n",
       "      <td>Greece</td>\n",
       "      <td>铜奖</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21694</th>\n",
       "      <td>Weightlifting</td>\n",
       "      <td>athens-1896</td>\n",
       "      <td>个人</td>\n",
       "      <td>Denmark</td>\n",
       "      <td>金奖</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21695</th>\n",
       "      <td>Weightlifting</td>\n",
       "      <td>athens-1896</td>\n",
       "      <td>个人</td>\n",
       "      <td>Great Britain</td>\n",
       "      <td>银奖</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21696</th>\n",
       "      <td>Weightlifting</td>\n",
       "      <td>athens-1896</td>\n",
       "      <td>个人</td>\n",
       "      <td>Greece</td>\n",
       "      <td>铜奖</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>21697 rows × 5 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "id": "de5f7c7c5c1ebf19",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T02:01:27.354584Z",
     "start_time": "2025-09-30T02:01:27.146193Z"
    }
   },
   "source": [
    "# 特征和标签\n",
    "X = dataset[['比赛类别', '比赛时间', '参与者', '国家']]\n",
    "y = dataset['奖牌']\n",
    "\n",
    "# 使用 One-Hot Encoding 对特征进行编码\n",
    "X_encoded = pd.get_dummies(X, drop_first=True)  # drop_first=True 避免多重共线性\n",
    "\n",
    "# 对目标变量（奖牌）进行标签编码\n",
    "le_award = LabelEncoder()\n",
    "y_encoded = le_award.fit_transform(y)\n",
    "\n",
    "# 划分训练集 60%，测试集 40%\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X_encoded, y_encoded, test_size=0.4, random_state=42, stratify=y_encoded\n",
    ")\n",
    "\n",
    "# 创建并训练高斯朴素贝叶斯模型\n",
    "gnb = GaussianNB()\n",
    "gnb.fit(X_train, y_train)\n",
    "\n",
    "# 在测试集上预测\n",
    "y_pred = gnb.predict(X_test)\n",
    "\n",
    "# 输出准确率\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(f\"模型在测试集上的准确率: {accuracy:.2%}\")"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型在测试集上的准确率: 33.67%\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9df503ac54a5c29a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
